
IddTankRespawnDialog = 1230;
IdcCbHumanRespawnList = 1231;
IdcTankMap= 1232;
IdcBtMatrixInsert =1233;
IdcCbWeaponList=1234;
IdcCbAmmoList=1235;
IdcBtAddWeapon=1236;
IdcBtAddAmmo=1237;
IdcCbCabList=1238;
IdcBtMatrixExtract=1239;
IdcCbHumanExtractList=1240;

IddTankDialog = 1230;


endDialog=false;
HumanRespawnList=[];


publicVariable "isPhoneAnswered";

ExtractCabNumber=-1;
_cabMarkers=[];


updateBtState={
	
	if(((lbCurSel IdcCbHumanRespawnList)>=0) and ((lbCurSel CbCabList)>=0) )then{
		ctrlEnable [IdcBtMatrixInsert,true];
	}else{
		ctrlEnable [IdcBtMatrixInsert,false];
		
	};

};

CbHumanRespawnList_SelChanged={
    //ctrlEnable [IdcBtMatrixInsert,true];
    lastDataSelected=lbText[IdcCbHumanRespawnList, lbCurSel IdcCbHumanRespawnList];
    format[" select : %1 %2",lastDataSelected,lbCurSel IdcCbHumanRespawnList] call debug; ;    
    
	call updateBtState;
};

CbCabList_SelChanged={
    
   
    format[" select cab : %1 ",lbCurSel CbCabList] call debug;    
	_index=lbCurSel CbCabList;
	selectedCab=CabsList select _index;
	publicvariable "selectedCab";
	
	deleteMarkerLocal "mSelectCab";
	
	_markerstr = createMarkerLocal ["mSelectCab",(getPos selectedCab)];
	_markerstr setMarkerShapeLocal "ICON";
	_markerstr setMarkerTypeLocal "Select";
	_markerstr setMarkerColorLocal "ColorGreen";
	
	
	ctrlMapAnimClear TankMap;
	
	TankMap ctrlMapAnimAdd [2,0.1,(getPos selectedCab)];
	
	ctrlMapAnimCommit TankMap;
	
	call updateBtState;
	
	
	// update extracttrigger
	
	_objBox = boundingBox selectedCab;
	_width =  abs ( ((_objBox select 0) select 0) - ((_objBox select 1) select 0) );
	_length = abs ( ((_objBox select 0) select 1) - ((_objBox select 1) select 1) );


	ExtractTrg setTriggerArea[_width*0.5,_length*0.5,(direction selectedCab),true];

	ExtractTrg setPos (getPos selectedCab);
	
	format["player pos : %1 , trg pos : %2 , list : %3",getpos player,getpos ExtractTrg,(getPos selectedCab),list ExtractTrg] call debug;
	
	if(isDebug)then{
		
		deleteMarkerlocal "toto";
		_markerstr = createMarkerLocal ["toto",(getPos selectedCab)];
		_markerstr setMarkerShapeLocal "RECTANGLE";
		_markerstr setMarkerSizeLocal [_width*0.7,_length*0.7];
		_markerstr setMarkerColorLocal "ColorRed";
		_markerstr setMarkerDirLocal (direction selectedCab);
	};
	
};

BtMatrixInsert_click={
	format ["test bt"] call debug;;
	_selectedHuman=nil;
	_value=lbCurSel IdcCbHumanRespawnList;
	format ["_value = %1",_value] call debug;; 
	_selectedHuman=HumanRespawnList select _value;
	format["_selectedHuman = %1",_selectedHuman] call debug;
	
	_selectedHuman setPos getPos(selectedCab);
	CbHumanRespawnList lbSetCurSel -1;
	
	if(isDebug)then { Objective setDammage 1;};
}; 

BtMatrixExtract_click={
	
	
	isRinging=true;
	publicvariable "isRinging";
	
	ctrlEnable [IdcBtMatrixExtract,false];	
	
	ctrlSetText [IdcBtMatrixExtract,"Le telephone sonne..."];	
	
	// if first click : lock cab choice and save it
	if(ExtractCabNumber<0)then{
		_value=lbCurSel CbCabList;
		CbCabList ctrlEnable false;
		
		ExtractCabNumber=_value;
		format["cab number %1",ExtractCabNumber] call debug;
		_marker=format["cab_%1",_value];
		
		_marker setMarkerColorLocal "ColorRed";
		"mSelectCab" setMarkerColorLocal "ColorBlack";
	};
	

}; 

BtAddWeapon_click={

	TankAddWeapon=lbCurSel IdcCbWeaponList;
	publicVariable "TankAddWeapon";
	
	format["TankAddWeapon = %1",TankAddWeapon] call debug;
	
	// testWP addWeaponCargo [((WeaponList select _value) select 1),1];
	// testWP addMagazineCargo [((WeaponList select _value) select 2),((WeaponList select _value) select 3)];
	
}; 

BtAddAmmo_click={

	TankAddAmmo=lbCurSel IdcCbAmmoList;
	publicVariable "TankAddAmmo";
	
	format["TankAddAmmo = %1",TankAddAmmo] call debug;
	
	// format["weapon = %1",(AmmoList select _value)] call debug;; 
	// testWP addMagazineCargo [(AmmoList select _value),10];
	
}; 



endDialog=false;

ExtractTrg=createTrigger[ "EmptyDetector",[0,0]]; 
ExtractTrg setTriggerActivation["ANY","PRESENT",true];
ExtractTrg setTriggerStatements["this", "", ""]; 


_c=0;

{// create cabs markers :	
			
	
	_markerstr = createMarkerLocal [format["cab_%1",_c],(getPos _x)];
	_markerstr setMarkerShapeLocal "ICON";
	_markerstr setMarkerTypeLocal "mil_box";
	_markerstr setMarkerColorLocal "ColorBlue";
	
	_c=_c+1;
	
}foreach CabsList;

		
	
while {true} do{

scopeName "dialogLoop";
	
	_TankRespawnDialog = createDialog "TankRespawnDialog";
    _TankDialog=findDisplay IddTankDialog;
		
    CbHumanRespawnList = (_TankDialog displayCtrl IdcCbHumanRespawnList);
	
	CbCabList = (_TankDialog displayCtrl IdcCbCabList);
	CbCabList = (_TankDialog displayCtrl IdcCbCabList);
	
	// getting controls from idc
	BtMatrixInsert = (_TankDialog displayCtrl IdcBtMatrixInsert);
	BtMatrixExtract = (_TankDialog displayCtrl IdcBtMatrixExtract);
	BtAddWeapon = (_TankDialog displayCtrl IdcBtAddWeapon);
	BtAddAmmo = (_TankDialog displayCtrl IdcBtAddAmmo);
	TankMap = (_TankDialog displayCtrl IdcTankMap);
	//CbHumanExtractList= (_TankDialog displayCtrl IdcCbHumanExtractList);;
	
	// attaching event to controls
    CbHumanRespawnList ctrlSetEventHandler ["LBSelChanged","['ListChange',_this] call CbHumanRespawnList_SelChanged"];
	CbCabList ctrlSetEventHandler ["LBSelChanged","['ListChange',_this] call CbCabList_SelChanged"];
    BtMatrixInsert ctrlSetEventHandler ["ButtonClick","['',_this] call BtMatrixInsert_click"];
	BtMatrixExtract ctrlSetEventHandler ["ButtonClick","['',_this] call BtMatrixExtract_click"];
	BtAddWeapon ctrlSetEventHandler ["ButtonClick","['',_this] call BtAddWeapon_click"];
	BtAddAmmo ctrlSetEventHandler ["ButtonClick","['',_this] call BtAddAmmo_click"];
	
	// launch background update script
    _HandleUpdateHumanList = CbHumanRespawnList execVM "Dialogs\TankRespawnDialog_UpdateHumanList.sqf"; 
	_HandleUpdateExtractList = CbHumanRespawnList execVM "Dialogs\TankRespawnDialog_UpdateExtractList.sqf"; 
	
    ctrlEnable [IdcBtMatrixInsert,false];
	ctrlEnable [IdcBtMatrixExtract,false];	
	
	{ // fill the cabs list
			_index=CbCabList lbAdd format["%1",_x];
			//CbCabList lbSetData [_index,_index+""];
	}foreach CabsList;	
	
	
	{ // fill the weapon list
		_index=lbAdd [IdcCbWeaponList,(_x select 0)];
        lbSetData [IdcCbWeaponList, (_x select 0)];
		
	}foreach WeaponList;
	
	{ // fill the ammo list
		_index=lbAdd [IdcCbAmmoList,_x ];
        lbSetData [IdcCbAmmoList, _x ];
		
	}foreach AmmoList;
	

	format["cab number %1 == nil ?",ExtractCabNumber] call debug;
	
	
	

	// re-set previous state (if dialog closed)
	if(ExtractCabNumber>=0 && isRinging)then{
	
		ctrlEnable [IdcBtMatrixExtract,false];		
		ctrlSetText [IdcBtMatrixExtract,"Le telephone sonne..."];	
	
		CbCabList lbSetCurSel ExtractCabNumber;
		CbCabList ctrlEnable false;
		
		_marker=format["cab_%1",ExtractCabNumber];
		"-------------- already ringing" call debug;
		_marker setMarkerColorLocal "ColorRed";
		"mSelectCab" setMarkerColorLocal "ColorBlack";
	}; 
	
	//----------------------------------
	
	waitUntil { (!dialog) or endDialog };
	
	// kill update threads
	terminate _HandleUpdateHumanList;
	terminate _HandleUpdateExtractList;
	
	"You exit the dialog" call debug;
	
	
    if(endDialog)then{
       
       breakOut "dialogLoop";
	   
    }else{
	
		sleep 4;
		
	};
};